# coding=utf-8
__author__ = '汤小洋'

# 导入pymysql模块
import pymysql

# 定义数据库连接配置
config = {
    'host': 'localhost',
    'port': 3306,
    'user': 'root',
    'password': 'root',
    'database': 'python',
    'charset': 'utf8'
}

# 获取连接
# conn = pymysql.connect(host='localhost', port=3306, user='root', password='root', database='python', charset='utf8')
conn = pymysql.connect(**config)

# 获取游标，相当于jdbc中的Statement
cursor = conn.cursor()

# 执行sql
sql = '''
        insert into t_user
          (username,password,age,height)
        values 
          (%s,%s,%s,%s)
    '''
num = cursor.execute(sql, ['李四', '123', 18, 180.5])  # 为占位符%s赋值，返回受影响的行数
print('受影响的行数：', num)

# 提交事务
conn.commit()

# 关闭资源
cursor.close()
conn.close()
